package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.OrdenCompraBean;

public class HibernateOrdenCompraDAO {
	private static HibernateOrdenCompraDAO instancia;
	private static SessionFactory sf;

	private HibernateOrdenCompraDAO() {

	}

	public static HibernateOrdenCompraDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateOrdenCompraDAO();
		}
		return instancia;
	}

	public void grabarOrdenCompra(OrdenCompraBean fb) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(fb);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public OrdenCompraBean buscarOrdenCompra(OrdenCompraBean fb) {
		Session session = sf.openSession();
		OrdenCompraBean f = (OrdenCompraBean) session.get(
				OrdenCompraBean.class, fb.getId());
		session.close();
		return f;
	}

	public List<OrdenCompraBean> list() {
		Session sesion = sf.openSession();
		sesion.beginTransaction();
		List<OrdenCompraBean> ordenes = sesion.createQuery(
				"from OrdenCompraBean").list();
		return ordenes;
	}
}
